package cn.ffcs.contract.web;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataIntegrityViolationException;
import com.opensymphony.xwork2.ActionContext;
import cn.ffcs.base.web.BaseAction;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.common.model.Attach;
import cn.ffcs.common.service.AttachService;
import cn.ffcs.common.service.IAttachService;
import cn.ffcs.contract.service.IProjectService;
import cn.ffcs.product.model.Product;
import cn.ffcs.product.service.ProductService;
import cn.ffcs.ref.model.ProductRefSort;
import cn.ffcs.ref.service.IProductRefSortService;
import cn.ffcs.sort.model.ProductSort;
import cn.ffcs.sort.service.IProductSortService;
import cn.ffcs.util.MyList;

/**
 * 合同管理<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">g.pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:2009-6-22</td>
 * <td>修改人:g.pany</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.pany
 * @version $Id$
 */
public class ProjectAction extends BaseAction
{
    /** 动态网路标识 */
    private static final long serialVersionUID = -8246991977511117520L;

    /** 合同服务 **/
    private IProjectService projectService;

    /** 查询结果集 **/
    private List resultList;

    /** 查询结果对象 **/
    private Map result;
    
    /** 查询条件ID **/
    private String objId;
    
    /** 办理类型 **/
    private String flowType;
    
    
    
    /**
     * 1、归档合同查询
     * @return
     */
    public String findArchiveContract()
    {
        if(pageControl==null){
            pageControl=new PageControl();
        }
        pageControl=projectService.findProjectContract(super.getConds(),pageControl);
        return SUCCESS;
    }

    /**
     * 2、合同会签进度查询
     * @return
     */
    public String findContractCountersigned()
    {
        if(pageControl==null){
            pageControl=new PageControl();
        }
        pageControl=projectService.findContractCountersigned(super.getConds(),pageControl);
        return SUCCESS;
    }

    /**
     * 3、合同预警
     * @return
     */
    public String findContractWarning()
    {
        if(pageControl==null){
            pageControl=new PageControl();
        }
        pageControl=projectService.findContractWarning(super.getConds(),pageControl);
        return SUCCESS;
    }

    /**
     * 4、合同模板查询
     * @return
     */
    public String findContractTemplate()
    {
        if(pageControl==null){
            pageControl=new PageControl();
        }
        pageControl=projectService.findContractTemplate(super.getConds(),pageControl);
        return SUCCESS;
    }

    /**
     * 5、经济合同查询
     * @return
     */
    public String findEconomicContracts()
    {
        if(pageControl==null){
            pageControl=new PageControl();
        }
        if(flowType==null||flowType.equals("")||flowType.equals("1")){   //代办理
            pageControl=projectService.findEconomicContracts(super.getConds(),pageControl);
        }else{      //已办理
            pageControl=projectService.findEconomicContractsByflow(super.getConds(),pageControl);
        }
        return SUCCESS;
    }
    
    /**
     * 查询合同详情
     * TODO 请输入方法说明
     * @return
     */
    public String findContractById(){
        if(objId!=null&&!"".equals(objId)){
            result=projectService.findObjectById(" project_contract t,project_task a ", "contractname,contractcode,partbname,applyuser,signdate,enddate,id", " where t.id=a.keyval and a.status='已归档' and id="+objId);
        }
        return SUCCESS;
    }
    
    public String findContractCountersignedById(){
        if(objId!=null&&!"".equals(objId)){
            String disCol="contractname,signcode,contractcode,partbname,applyuser,tradesum,signdate,frameflag,enddate,contracttype,ccontracttype,creadate,applydept,id,signenddate,archiveid,dept,status,userid,username,areacode,keyval,receivetime,opertype,opertypename";
            result=projectService.findObjectById(" project_contract t,project_task a ",disCol, " where t.id=a.keyval and id="+objId);
        }
        return SUCCESS;
    }
    
    /**
     * 
     * TODO 请输入方法说明
     * @return
     */
    public String findContractTemplateById(){
        if(objId!=null&&!"".equals(objId)){
            String disCol="dispsort,templatename,templateid,areacode,area,userid,username,fileid,frameflag,createdate,companyflag,upfile";
            result=projectService.findObjectById(" project_docu_template ",disCol," where templateid="+objId);
        }
        return SUCCESS;
    }    
    
    /**
     * 文件下载
     * @return
     * @throws Exception
     */
    public String showFile() throws Exception
    {
        findContractTemplateById();
        if(result==null){
            return null;
        }
        String fileName = result.get("templatename").toString();

        ActionContext ctx = ActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) ctx
                .get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response = (HttpServletResponse) ctx
                .get(ServletActionContext.HTTP_RESPONSE);
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;" + " filename="
                + new String(fileName.getBytes(), "UTF-8"));
        java.sql.Blob b=(java.sql.Blob)result.get("upfile");
        if(b.length()>0){
            write(response.getOutputStream(),b.getBytes(1, (int)b.length()));
        }

        return null;
    }
    
    /**
     * 输出文件内容
     * @param os
     * @param content
     */
    private void write(OutputStream os, byte[] content)
    {
        try
        {
            os.write(content);
            os.flush();
        }
        catch (IOException ex)
        {
            throw new RuntimeException(ex);
        }
    }
    
    
    public IProjectService getProjectService()
    {
        return projectService;
    }

    public void setProjectService(IProjectService projectService)
    {
        this.projectService = projectService;
    }

    public List getResultList()
    {
        return resultList;
    }

    public void setResultList(List resultList)
    {
        this.resultList = resultList;
    }

    public Map getResult()
    {
        return result;
    }

    public void setResult(Map result)
    {
        this.result = result;
    }

    public String getObjId()
    {
        return objId;
    }

    public void setObjId(String objId)
    {
        this.objId = objId;
    }

    public String getFlowType()
    {
        return flowType;
    }

    public void setFlowType(String flowType)
    {
        this.flowType = flowType;
    }




}
